# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=angleproject
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.r20194.6c41793f
pkgrel=4
pkgdesc='A conformant OpenGL ES implementation for Windows, Mac, Linux, iOS and Android (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://chromium.googlesource.com/angle/angle'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-rapidjson"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gn"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
_commit=("6c41793f39e53eb0ed33ff18cc3ed2a37cc97c65")
options=('!buildflags')
source=("${_realname}::git+https://chromium.googlesource.com/angle/angle.git#commit=${_commit}"
        "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=32324ec2c551579f3b226a23a7a05d8545998399"
        "bare-clones/zlib::git+https://chromium.googlesource.com/chromium/src/third_party/zlib.git#commit=d866d41e168ec04545bb17031fa911007a0581f4"
        "bare-clones/clang::git+https://chromium.googlesource.com/chromium/src/tools/clang.git#commit=1b8faee860dd0c549bf5070ff54a410c0370d075"
        001-add-mingw-toolchain.patch
        002-buildflags-fixes.patch
        003-angle-src-fixes.patch
        jpeg.gn
        jsoncpp.gn
        png.gn
        rjson.gn
        zlib.gn)
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '71f7cf9110a2a3b7fb5c4117bc4aca0629839da024c8320a1ffd4b76f893ce32'
            'd73b808fe69914ee9d9934fad79389706af2c1ac280c1c0296d1046c482febad'
            'a096c2d8bb458c372998c209fef8ebd51b65c6410ad70aed627f230f3dec7b03'
            'bf878de3203a62e2a7f81bd4cd79237adc74804b4b692d9277da005ed56d4f2a'
            '86013781c2700219d4f64d7ac34ad16c40fcca9a641371385f67f642e23c643b'
            'f0fb05348bf2de599eff35e2d35e3336b9720b6bd2799af9d186c05ff45d34f0'
            '044afff420a1c8ad896f79fb8ae03878f6063bdb0a25dab0b8e4bff092e3d3ac'
            '4dc3baf79ba7c95b52cbbafce81b26e6d165d4ae7e0dcc2eabe0250466edf178')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd ${_realname}
  local _major=$(head -n 14 src/common/angle_version.h | grep 'ANGLE_MAJOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 14 src/common/angle_version.h | grep 'ANGLE_MINOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  printf "%s.%s.r%s.%s" "$_major" "$_minor" "$(git rev-list --count HEAD)" "$(git rev-parse --short=8 "${_commit}")"
}

prepare() {
  echo "Patching build directory"
  cd ${srcdir}/build
  patch -p1 -i ${srcdir}/001-add-mingw-toolchain.patch
  python ${srcdir}/build/util/lastchange.py -o ${srcdir}/build/util/LASTCHANGE
  rm -rf ${srcdir}/${_realname}/build
  ln -sf ${srcdir}/build ${srcdir}/${_realname}
  echo "checkout_google_benchmark = false" > ${srcdir}/${_realname}/build/config/gclient_args.gni

  echo "Patching zlib directory"
  mkdir -p ${srcdir}/${_realname}/third_party/zlib
  ln -sf ${srcdir}/zlib.gn ${srcdir}/${_realname}/third_party/zlib/BUILD.gn
  ln -sf ${srcdir}/zlib/google ${srcdir}/${_realname}/third_party/zlib

  echo "Soft linking extra repos"
  mkdir -p ${srcdir}/${_realname}/third_party/libjpeg
  ln -sf ${srcdir}/jpeg.gn ${srcdir}/${_realname}/third_party/libjpeg/BUILD.gn
  mkdir -p ${srcdir}/${_realname}/third_party/jsoncpp
  ln -sf ${srcdir}/jsoncpp.gn ${srcdir}/${_realname}/third_party/jsoncpp/BUILD.gn
  rm -f ${srcdir}/${_realname}/third_party/libpng/BUILD.gn
  ln -sf ${srcdir}/png.gn ${srcdir}/${_realname}/third_party/libpng/BUILD.gn
  rm -f ${srcdir}/${_realname}/third_party/rapidjson/BUILD.gn
  ln -sf ${srcdir}/rjson.gn ${srcdir}/${_realname}/third_party/rapidjson/BUILD.gn
  ln -sf ${srcdir}/clang ${srcdir}/${_realname}/tools

  echo "Patching angle source"
  cd ${srcdir}/${_realname}
  apply_patch_with_msg \
  002-buildflags-fixes.patch \
  003-angle-src-fixes.patch

  echo "Generating angle_commit.h"
  python src/commit_id.py gen src/common/angle_commit.h
}

build() {
  cd ${_realname}

  local _target=Release
  local _debug=false
  if check_option "debug" "y"; then
    _target=Debug
    _debug=true
  fi

  local _arch=x64
  if [[ ${CARCH} == aarch64 ]]; then
    _arch=arm64
  elif [[ ${CARCH} == i686 ]]; then
    _arch=x86
  fi

  if [[ ${MSYSTEM} == CLANG* ]]; then
    export CXX=clang++
    export CC=clang
    export LD=clang++
    export AR=llvm-ar
  else
    export CXX=g++
    export CC=gcc
    export LD=g++
    export AR=ar
  fi

  ${MINGW_PREFIX}/bin/gn gen out/${_target}-${MSYSTEM} --args="
    target_cpu=\"${_arch}\"
    is_debug=${_debug}
    is_component_build=true
    angle_enable_abseil=false
    angle_enable_metal=false
    angle_enable_d3d11_compositor_native_window=false
    angle_enable_cl=false
    chrome_pgo_phase=0
    use_custom_libcxx=false
    use_sysroot=false
    treat_warnings_as_errors=false"

  ${MINGW_PREFIX}/bin/ninja -C out/${_target}-${MSYSTEM}
}

package() {
  cd ${_realname}

  local _target=Release
  if check_option "debug" "y"; then
    _target=Debug
  fi

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,include}

  install -Dm755 out/${_target}-${MSYSTEM}/lib*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  install -Dm755 out/${_target}-${MSYSTEM}/lib*.dll.a ${pkgdir}${MINGW_PREFIX}/lib/
  install -Dm755 out/${_target}-${MSYSTEM}/obj/lib*.a ${pkgdir}${MINGW_PREFIX}/lib/
  cp -rf include/* ${pkgdir}${MINGW_PREFIX}/include/

  # Remove KHR include, fixes conflict with mingw-w64
  rm -rf ${pkgdir}${MINGW_PREFIX}/include/KHR
  # Remove OpenCL headers to avoid file conflict with opencl-headers
  rm -rf ${pkgdir}${MINGW_PREFIX}/include/CL

  install -Dm644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
